package com.bicon.botu.mall.coupon.api;

import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.coupon.common.CouponStatus;
import com.bicon.botu.mall.coupon.entity.Coupons;
import com.bicon.botu.mall.coupon.service.IMyCouponsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 我的优惠券 前端对接接口
 * </p>
 *
 * @author 吕祥
 * @since 2017-07-19
 */
@RestController
@RequestMapping(value = "/V1.0.0/api/MyCoupons")
public class MyCouponsApi extends BaseController{

    @Autowired
    IMyCouponsService iMyCouponsService;

    /**
     * 新增我的优惠卷
     *
     * @param userId    用户id
     * @param couponsId 优惠券id
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String userId, @RequestParam String couponsId) {
        if (StringUtils.isAnyBlank(userId, couponsId)) {
            return sendResult400();
        }
        return iMyCouponsService.add(userId, couponsId);
    }

    /**
     * 删除我的优惠券
     *
     * @param userId     用户id
     * @param couponsIds 优惠券 id Json格式
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public BaseResult deleteById(@RequestParam String userId, @RequestParam String couponsIds) {
        if (StringUtils.isAnyBlank(userId, couponsIds)) {
            return sendResult400();
        }
        return iMyCouponsService.deleteById(userId, couponsIds);
    }

    /**
     * 修改我的优惠券
     *
     * @param id        我的优惠券id
     * @param userId    用户id
     * @param couponsId 优惠券id
     * @return
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public BaseResult updateById(@RequestParam String id, @RequestParam String userId, @RequestParam String couponsId) {
        if (StringUtils.isAnyBlank(id, userId, couponsId)) {
            return sendResult400();
        }
        return iMyCouponsService.updateById(id, userId, couponsId);
    }

    /**
     * 查询用户的优惠券(未使用， 已使用， 已过期三种优惠券信息)
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping(value = "/findAllMyCoupons", method = RequestMethod.POST)
    public BaseResult findAllMyCoupons(@RequestParam String userId) {
        if (StringUtils.isAnyBlank(userId)) {
            return sendResult400();
        }
        Map<String, List<Coupons>> map = iMyCouponsService.findAllMyCoupons(userId);
        return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200, map);
    }

    /**
     * .根据商品查询该商品可用优惠券
     *
     * @param userId       用户id
     * @param classifyPath 分类path
     * @param brandId      品牌id
     * @param productId    商品id
     * @return
     */
    @RequestMapping(value = "/findCoupon", method = RequestMethod.POST)
    public BaseResult findCoupon(@RequestParam String userId, @RequestParam String classifyPath,
                                 @RequestParam String brandId, @RequestParam String productId) {
        if (StringUtils.isAnyBlank(userId, classifyPath, brandId, productId)) {
            return sendResult400();
        }
        Map<String, List<Coupons>> resultMap = iMyCouponsService.findCoupon(userId, classifyPath, brandId, productId);
        return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200, resultMap);
    }


    /**
     * 根据多个商品查询这些商品可用的用户已领取的优惠券
     *
     * @param userId 用户id
     * @param paths  Json格式的path集合
     * @return
     */
    @RequestMapping(value = "/multiFindCoupons", method = RequestMethod.POST)
    public BaseResult multiFindCoupons(@RequestParam String userId, @RequestParam String paths) {
        if (StringUtils.isAnyBlank(userId, paths)) {
            return sendResult400();
        }
        Map<String, List<Coupons>> resultMap = iMyCouponsService.multiFindCoupons(userId, paths);
        return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200, resultMap);
    }

    /**
     * 根据多个商品查询这些商品可用的用户已领取的优惠券 (安卓)
     *
     * @param userId 用户id
     * @param paths  Json格式的path集合
     * @return
     */
    @RequestMapping(value = "/findMyUseCoupons", method = RequestMethod.POST)
    public BaseResult findMyCouponsByMultiProduct(@RequestParam String userId, @RequestParam String paths) {
        if (StringUtils.isAnyBlank(userId, paths)) {
            return new BaseResult(CouponStatus.RESPONSE_CODE_400, CouponStatus.RESPONSE_MESSAGE_400);
        }
        return new BaseResult(CouponStatus.RESPONSE_CODE_200, CouponStatus.RESPONSE_MESSAGE_200,
                                    iMyCouponsService.findMyCouponsByMultiProduct(userId, paths));
    }
}
